﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;

namespace SaleOnlineSystem.Controls
{
    public partial class EditProfile : System.Web.UI.UserControl
    {
        public string Username;
        public string Password;
        public string PasswordMahoa;
        public string Email;
        public string FullName;
        public string Phonenumber;
        public string Address;
        public string username;
        public int user_id;
        public string emailemployee;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] == null)
            {
                Response.Redirect("/trangchu.htm");
            }
            if (!IsPostBack)
            {
                username = Session["Username"].ToString();
                // user_ida = Convert.ToInt32(Session["Employee_ID"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Employee", "Employee_User", username, "selectemployee");
                lblUsername.Text = dt.Rows[0]["Employee_User"].ToString();
                lblEmail.Text = txtEmail.Text = dt.Rows[0]["Employee_Email"].ToString();
                lblFullname.Text = txtFullname.Text = dt.Rows[0]["Employee_FullName"].ToString();
                lblMobile.Text = txtMobile.Text = dt.Rows[0]["Employee_Phonenumber"].ToString();
                lblAddress.Text = txtAddress.Text = dt.Rows[0]["Employee_Address"].ToString();
                imgAvatar.ImageUrl = dt.Rows[0]["Employee_Avatar"].ToString();
            }
        }


        protected void lbtFullname_Click(object sender, EventArgs e)
        {
            txtFullname.Enabled = true;
            btnUpdate.Visible = true;
            //btnCancel.Visible = true;
        }


        protected void lbtAddress_Click(object sender, EventArgs e)
        {
            txtAddress.Enabled = true;
            btnUpdate.Visible = true;
            //btnCancel.Visible = true;
        }

        protected void lbtMobile_Click(object sender, EventArgs e)
        {
            txtMobile.Enabled = true;
            btnUpdate.Visible = true;
            //btnCancel.Visible = true;
        }

        protected void lbtAvatar_Click(object sender, EventArgs e)
        {
            imgAvatar.Visible = false;
            fulAvatar.Enabled = true;
            btnUpdate.Visible = true;
            //btnCancel.Visible = true;
        }

        protected void lbtEmail_Click(object sender, EventArgs e)
        {
            txtEmail.Enabled = true;
            btnUpdate.Visible = true;
            //btnCancel.Visible = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.ToString());
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //string emailemployee = Session["Employee_User"].ToString();
            ////string user_id = Session["Employee_ID"].ToString();
            string email = txtEmail.Text;
            string fullname = txtFullname.Text;
            string address = txtAddress.Text;
            string mobile = txtMobile.Text;
            string filePath = null;
            string Avatar = "";
            string username = Session["Username"].ToString();
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Employee", "Employee_User", username, "selectemployee");
            Avatar = dt.Rows[0]["Employee_Avatar"].ToString();
            int idemp = Convert.ToInt32(dt.Rows[0]["Employee_ID"].ToString());

            if (fulAvatar.HasFile)
            {
                if (LIB.Validate.CheckFileType(fulAvatar.FileName))
                {
                    string link = fulAvatar.FileName;
                    LIB.Validate saveimage = new LIB.Validate();
                    filePath = "/Images/Image-User/" + idemp + Path.GetExtension(fulAvatar.FileName) + "";
                    saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-User/"), idemp + Path.GetExtension(fulAvatar.FileName), 150, fulAvatar.PostedFile.InputStream);
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                    cl[0] = new DAL.DHelpers.Column("Employee_Email", email, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[1] = new DAL.DHelpers.Column("Employee_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[2] = new DAL.DHelpers.Column("Employee_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[3] = new DAL.DHelpers.Column("Employee_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[4] = new DAL.DHelpers.Column("Employee_Avatar", filePath, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                    string[] colWhere = new string[1];
                    colWhere[0] = "Employee_User";

                    object[] colValue = new object[1];
                    colValue[0] = username;

                    DAL.DCollector.DBCollector.Instance.Update("tbl_Employee", cl, colWhere, colValue);
                    //Response.Redirect("/trangchu.htm");
                    Response.Redirect("/thongtincanhan.htm");

                }
                else
                {
                    lblCheckFile.Text = "Chỉ có thể upload file(.jpg,.png,.jpeg)";
                }
            }
            else
            {
                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                cl[0] = new DAL.DHelpers.Column("Employee_Email", email, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                cl[1] = new DAL.DHelpers.Column("Employee_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                cl[2] = new DAL.DHelpers.Column("Employee_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                cl[3] = new DAL.DHelpers.Column("Employee_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                string[] colWhere = new string[1];
                colWhere[0] = "Employee_User";

                object[] colValue = new object[1];
                colValue[0] = username;

                DAL.DCollector.DBCollector.Instance.Update("tbl_Employee", cl, colWhere, colValue);
                //Response.Redirect("/trangchu.htm");
                Response.Redirect("/thongtincanhan.htm");
            }
        }
    }
}